/*
 * 创建日期 2010-1-11
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.bean;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import net.sf.json.JSONObject;

/**
 *  工具栏
 * @author: 王文成
 * @version: 1.0
 * @since 2010-1-11
 */
public class Toolbar {

    public static final String BUTTON_COL = "col";

    public static final String BUTTON_BACK = "back";

    public static final String BUTTON_FORWARD = "forward";

    public static final String BUTTON_EXPORT = "export";

    private String json;

    private Set<String> buttons = new HashSet<String>();{
        buttons.add(BUTTON_COL);
        buttons.add(BUTTON_BACK);
        buttons.add(BUTTON_FORWARD);
        buttons.add(BUTTON_EXPORT);
    }

    private boolean visible = false;

    public Toolbar() {
        
    }
    
    public Toolbar(String json) {
        this.json = json;
        this.init();
    }

    private void init() {
        if (json.startsWith("{") || json.equals("show")) {
            visible = true;
            JSONObject bean = JSONObject.fromObject(json);
            Iterator iter = bean.keys();
            while (iter.hasNext()) {
                save(bean, iter.next().toString());
            }
        }
    }

    private void save(JSONObject bean, String col) {
        String value = bean.getString(col);
        if (value.equalsIgnoreCase("false"))
            buttons.remove(col);
    }

    /**
     * 取得有效的按钮
     * 
     * @return
     */
    public Set<String> getButtons() {
        return buttons;
    }

    /**
     * 是否显示工具栏
     * 
     * @return
     */
    public boolean isVisible() {
        return visible;
    }
}
